import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;

public class GameServer {
	public static final int PORT = 2000;
	/** Main server program keep accepting new players after connected 
	 * @param args Server requires three arguments, see message for further instructions
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException 
	{
		/* Server requires 3 arguments, and if the condition is not met, the user will get some information */
		if(args.length < 3){
			System.out.println("Paper-soccer Server\nArgument that are requiered to start is:\nargs[0] = int Gameplan(sizeX)\n"
					+ "args[1] = int Gameplan(sizeY)\nargs[2] = char s/d || string single/dual (player mode)\n\n"
					+ "Example> GameServer 8 10 d \n\nNOTE: Gameplan is to be inserted with (even) numbers" );
			return;
		}

		/* Open up a server socket for receiving connections from client and AI */
		ServerSocket s = new ServerSocket(PORT);
		System.out.println("Server socket: " + s);
		System.out.println("Server listening...");

		/* Check if the args[2] is set to any form of dual player mode */
		if(args[2].equals("d") || args[2].equals("dual") || args[2].equals("D") || args[2].equals("Dual") || args[2].equals("DUAL"))
		{
			// Console text to see the arguments passed
			System.out.println("Server going in for (Dual) player mode\nPassed arguments:\nargs[0] = " + args[0] + "\nargs[1] = " + args[1] + "\nargs[2] = " + args[2]);

			try {
				while(true) { // Blocks until a connection occurs 
					Socket player_socket1 = s.accept();
					System.out.println("Connection accepted.");
					System.out.println("The new socket: " + player_socket1);
					Socket player_socket2 = s.accept();
					System.out.println("Secound connection accepted.");
					System.out.println("The new socket: " + player_socket2);

					// Trying to create an GameThread 
					try { 
						// 2*(Integer.parseInt(args[0])/2)
						// Get rid of uneven Grid sizes
						GameThread t = new GameThread(player_socket1, player_socket2, 2*(Integer.parseInt(args[0]) / 2), 2*(Integer.parseInt(args[1]) / 2));
						System.out.println("New thread started."); // Debug text to console
						System.out.println("The new thread: " + t);
					}
					catch(IOException e) {
						player_socket1.close();
						player_socket2.close(); // Guess it gets error if not this one is connected
					}
				}
			}
			finally { s.close(); }
		} // end (dual) player
		else
		{
			// Console text to see the arguments passed
			System.out.println("Server going in for (Single) player mode\nPassed arguments:\nargs[0] = " + args[0] + "\nargs[1] = " + args[1] + "\nargs[2] = " + args[2]);

			try { 
				while(true) { // Blocks until a connection occurs 
					Socket player_socket = s.accept();
					System.out.println("Connection accepted.");
					System.out.println("The new socket: " + player_socket);
					System.out.println("Enstablish connection with AI."); // Debug text to console
					GameAI AI = new GameAI();
					Socket AI_socket = s.accept();
					System.out.println("AI Connected."); // Debug text to console
					AI.start();

					// Trying to create an GameThread 
					try { 
						GameThread t = new GameThread(player_socket, AI_socket, Integer.parseInt(args[0]), Integer.parseInt(args[1])); // Create a new socket for the client, (forking)
						System.out.println("New thread started."); // Debug text to console
						System.out.println("The new thread: " + t);
					}
					catch(IOException e) {
						// If the constructor fails, close the socket,
						// otherwise the thread will close it:
						player_socket.close();
						AI_socket.close();
					} 
				}
			}
			finally { s.close(); }
		}

	}
}
